package com.founder.bdyx.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.founder.bdyx.core.util.SpringContextHolder;
import com.founder.bdyx.modules.monitor.util.DateUtils;
import com.founder.bdyx.modules.sys.mapper.ApplyInfoMapper;
import com.founder.bdyx.modules.sys.model.ApplyInfo;
import com.founder.bdyx.wxpro.conf.WxConfig;
import com.founder.bdyx.wxpro.util.HttpUtil;
import com.founder.bdyx.wxpro.util.VerifyUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 通知缴费定时任务
 */
@Component
public class OrderPayNoticeTask implements Job {
    Log log = LogFactory.getLog(OrderPayNoticeTask.class);

    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private ApplyInfoMapper mapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        log.info("缴费任务通知。。");
        List<ApplyInfo> al = mapper.getAllPayOrder("1","0");
        if (al == null || al.size() <= 0) return;

        String accessToken = VerifyUtil.getAccessToken(SpringContextHolder.getServletContext());
        log.info("accessToken : " + accessToken);

        for (ApplyInfo applyInfo : al){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("touser",applyInfo.getUserId());
            jsonObject.put("template_id",wxConfig.applyPass);
            String url = wxConfig.basePath + "/wxpro/redirect?index=4"+applyInfo.getUuid();
            jsonObject.put("url",url);

            JSONObject data = new JSONObject();
            jsonObject.put("data",data);

            JSONObject first = new JSONObject();
            first.put("value","您有审核通过的申请单，请尽快进行支付操作!");
            data.put("first",first);

            JSONObject keyword1 = new JSONObject();//订单编号
            keyword1.put("value",applyInfo.getUuid());
            keyword1.put("color","#173177");
            data.put("keyword1",keyword1);

            JSONObject keyword2 = new JSONObject();//商品名称
            keyword2.put("value","病历档案 " + applyInfo.getPrintCount() + "份");
            keyword2.put("color","#173177");
            data.put("keyword2",keyword2);

            JSONObject keyword3 = new JSONObject();//待支付金额
            keyword3.put("value",applyInfo.getPayPrice() + " 元");
            keyword3.put("color","#173177");
            data.put("keyword3",keyword3);

            JSONObject keyword4 = new JSONObject();//购买时间
            keyword4.put("value", DateUtils.getYyyyMmDdHhMm());
            keyword4.put("color","#173177");
            data.put("keyword4",keyword4);

            String notice = HttpUtil.sendPost(wxConfig.templateUrl.replace("ACCESS_TOKEN",accessToken),jsonObject.toString());
            log.info("审核通过模板消息返回参数：" + notice);

            JSONObject noticeJSON = JSON.parseObject(notice);
            if("0".equals(noticeJSON.getString("errcode"))){//消息通知成功后 更新状态
                applyInfo.setNoticeFlag("1");
                mapper.updatePayOrder(applyInfo);
            }
        }
    }
}
